﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using RuoVea.GoFastdfs.Web.Controllers;
using RuoVea.GoFastdfs.Common;
using RuoVea.GoFastdfs.Entity;
using RuoVea.GoFastdfs.Service.Inserface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RuoVea.GoFastdfs.Web.Controllers
{
    public class HomeController : BaseController
    {
        IIndexService indexService;
        IRuoveadService ruoveadService;
        IConfiguration _configuration;
        string fastUri = string.Empty;
        public HomeController(IConfiguration configuration, IIndexService _indexService, IRuoveadService _ruoveadService)
        {
            _configuration = configuration;
            fastUri = configuration["gofastdfs:url"];
            indexService = _indexService;
            ruoveadService = _ruoveadService;
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            
            ViewData["osName"]= Environment.MachineName;
            ViewData["osVersionName"] = GetOsVersion(Environment.OSVersion.Version);
            ViewData["version"] = _configuration["version"];
            ViewData["versionDate"] = _configuration["versionDate"];
            return View();
        }
                private string GetOsVersion(Version ver) {
            string strClient = "";
            if (ver.Major == 5 && ver.Minor == 1)
            {
                strClient = "Win XP";
            }
            else if (ver.Major == 6 && ver.Minor == 0)
            {
                strClient = "Win Vista";
            }
            else if (ver.Major == 6 && ver.Minor == 1)
            {
                strClient = "Win 7";
            }
            else if (ver.Major == 5 && ver.Minor == 0)
            {
                strClient = "Win 2000";
            }
            else
            {
                strClient = "未知";
            }
            return strClient;
        }
        /// <summary>
        /// 获取状态信息
        /// </summary>
        /// <returns></returns>
        public IActionResult getStatus()
        {
            try
            {
                String json = HttpHelper.HttpGet(fastUri + FinalAPI.API_STATUS);
                JObject parseObj = json.ToEntity<JObject>();
                if (parseObj.GetValue("status").ToString().Equals(FinalAPI.API_STATUS_SUCCESS))
                {
                    string result = indexService.getStatus(parseObj.GetValue("data").ToString()).ToJson();
                    return Content(ResponseBean.success(result).ToJsonString());
                }
                else
                {
                    return Content(ResponseBean.fail("调取go-fastdfs接口失败").ToJsonString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            return Content(ResponseBean.fail("系统异常").ToJsonString());
        }

        /// <summary>
        /// 修正统计信息(30天)
        /// </summary>
        /// <returns></returns>
        public IActionResult repair_stat()
        {
            int count = 0;
            for (int i = 0; i > -30; i--)
            {
                String dateStr = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
                JObject map = new JObject(16);
                map.Add("date", dateStr);
                String result = HttpHelper.HttpPost(fastUri + FinalAPI.API_REPAIR_STAT, map.ToJson());
                JObject parseObj = result.ToObject<JObject>();
                if (parseObj.GetValue("status").Equals("ok"))
                {
                    count++;
                }
            }
            return Content(ResponseBean.success("成功修正" + count + "天数据", null).ToJsonString());
        }

        /// <summary>
        /// 删除空目录
        /// </summary>
        /// <returns></returns>
        public IActionResult remove_empty_dir()
        {
            String result = HttpHelper.HttpPost(fastUri + FinalAPI.API_REMOVE_EMPTY_DIR);
            JObject parseObj = result.ToObject<JObject>();
            if (parseObj.GetValue("status").Equals(FinalAPI.API_STATUS_SUCCESS))
            {
                return Content(ResponseBean.success("操作成功,正在后台操作,请勿重复使用此功能", null).ToJsonString());
            }
            else
            {
                return Content(ResponseBean.fail("操作失败,请稍后再试").ToJsonString());
            }
        }

        /// <summary>
        /// 备份元数据, 30天
        /// </summary>
        /// <returns></returns>
        public IActionResult backup()
        {
            int count = 0;
            for (int i = 0; i > -30; i--)
            {
                String subDateStr = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
                String dateStr = DateTime.Now.ToString("yyyyMMdd");
                JObject map = new JObject(16);
                map.Add("date", dateStr);
                String result = HttpHelper.HttpPost(fastUri + FinalAPI.API_BACKUP, map.ToJson());
                JObject parseObj = result.ToObject<JObject>();
                if (parseObj.GetValue("status").Equals(FinalAPI.API_STATUS_SUCCESS))
                {
                    count++;
                }
            }
            return Content(ResponseBean.success("成功备份" + count + "天数据", null).ToJsonString());
        }

        /// <summary>
        /// 同步失败修复
        /// </summary>
        /// <returns></returns>
        public IActionResult repair()
        {
            String result = HttpHelper.HttpPost(fastUri + FinalAPI.API_REPAIR + "?force=1");
            JObject parseObj = result.ToObject<JObject>();
            if (parseObj.GetValue("status").Equals(FinalAPI.API_STATUS_SUCCESS))
            {
                return Content(ResponseBean.success("操作成功,正在后台操作,请勿重复使用此功能", null).ToJsonString());
            }
            else
            {
                return Content(ResponseBean.fail("操作失败,请稍后再试").ToJsonString());
            }
        }

        /// <summary>
        /// 获取所有集群
        /// </summary>
        /// <returns></returns>
        public IActionResult getAllRuovead()
        {
            List<RuoveadEntiy> ruoveads = ruoveadService.list();
            return Content(ResponseBean.success(ruoveads).ToJsonString());
        }

        /// <summary>
        /// 切换集群
        /// </summary>
        /// <param name="id"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public IActionResult switchRuovead(int id)
        {
            //if (id == getPeers().getId())
            {
                return Content(ResponseBean.fail("当前正在使用此集群").ToJsonString());
            }
            //UserEntity user = new UserEntity();
            //user.setPeersId(id);
            //user.setId(getUser().getId());
            //user.setUpdateTime(new Date());
            //if (userService.updateById(user))
            //{
            //    RuoveadEntiy peers = ruoveadService.getById(id);
            //    session.setAttribute("peers", peers);
            //    return Content(ResponseBean.success().ToJsonString());
            //}
            //return Content(ResponseBean.fail("切换失败").ToJsonString());
        }
    }
}
